﻿/**
 * Copyright © TnT-VNGroup 2011
 * PROJECT:   SmartCollector
 * $HeadURL: http://smartcollector.googlecode.com/svn/branches/Srcs/SmartCollector.Domain/Repository/ItemRepository.cs $
 * $Author: chithanh12@gmail.com $
 * $Date: 2011-05-09 04:17:33 +0000 (Mon, 09 May 2011) $
 * $Rev: 117 $
 **/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using SmartCollector.Domain.Entity;

namespace SmartCollector.Domain.Repository
{
    public class ItemRepository
    {
        #region Fields
        #endregion Fields

        #region Properties
        #endregion Properties

        #region Constructors
        #endregion Constructors

        #region Methods

        #region Public Methods
        #endregion Public Methods

        #region Private Methods
        #endregion Private Methods

        #endregion Methods

        /// <summary>
        /// 
        /// </summary>
        private Table<tbl_Item> m_items;

        /// <summary>
        /// Gets the items.
        /// </summary>
        public IQueryable<tbl_Item> Items
        {
            get
            {
                return m_items.AsQueryable();
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemRepository"/> class.
        /// </summary>
        public ItemRepository()
        {
            m_items = (new EntityDomainDataContext()).GetTable<tbl_Item>();
        }

        /// <summary>
        /// Determines whether the specified category id is exist.
        /// </summary>
        /// <param name="categoryId">The category id.</param>
        /// <param name="siteId">The site id.</param>
        /// <param name="time">The time.</param>
        /// <returns>
        ///   <c>true</c> if the specified category id is exist; otherwise, <c>false</c>.
        /// </returns>
        public bool IsExist(int categoryId, int siteId, DateTime time)
        {
            return m_items.Where
                (c => c.CategoryID == categoryId && c.ReferenceSite == siteId &&
                c.Date == time).FirstOrDefault() != null;
        }

        /// <summary>
        /// Adds the item.
        /// </summary>
        /// <param name="item">The item.</param>
        public void AddItem(tbl_Item item)
        {
            m_items.InsertOnSubmit(item);
        }

        /// <summary>
        /// Removes the itemsby category.
        /// </summary>
        /// <param name="cateId">The cate id.</param>
        public void RemoveItemsbyCategory(int cateId)
        {
            var deleteItems = m_items.Where(p => p.CategoryID == cateId);
            if(deleteItems!=null)
            {
                m_items.DeleteAllOnSubmit(deleteItems);
            }
        }

        /// <summary>
        /// Submits the changes.
        /// </summary>
        public void SubmitChanges()
        {
            m_items.Context.SubmitChanges();
        }
    }
}
